// const rp = require('request-promise')
// const axios = require('axios')
const sc_DBBASE = require('../models/sc_day_total_money.js')
const sc_DINGDAN = require('../models/sc_dingdan.js')

const rp = require('request-promise')
var config = require("../utils/config");
// var intf = require("../utils/interface");
const db = require('../db')
const {
	Op
} = require("sequelize");
const Sequelize = require('sequelize')
async function  get_day_money(date){
	// const date = new Date(date1)
	// const Y = date.getFullYear() // 年
	// const M = (date.getMonth() + 1).toString().padStart(2, '0');
	// const D = date.getDate().toString().padStart(2, '0');
	// var nowDate = `${Y}${M}${D}`
	var sdata = {
		addTime:date
	}
	
	const totalAmount = await sc_DINGDAN.findAll({
		
		attributes: [
		  ['date','addTime'],
		  
		  [Sequelize.fn('sum', Sequelize.col('money2')), 'money3'],
		],
		group: ['addTime'],
		
		// attributes: {
		// 	//排除之前没有字段'createdAt',
		// 	exclude: ['id', 'updatedAt', 'version']
		// },
		// sort: ['date', 'descending'],
		order: [['date', 'DESC']],
		limit: 10000,
		// offset: (Number(page) - 1) * limit,
		where: sdata,
	});
	return totalAmount[0].dataValues.money3
}
module.exports = function(router) {
	
	router.get('/sc/order/sc_day_total_money', async (ctx) => {
		// var {
		// 	data
		// } = ctx.request.body;
		// var data = ctx.request.body;
		// data.date = new Date().getTime()
		var sdata = {
			addTime:"2023-06-16"
		}
		const totalAmount = await sc_DINGDAN.findAll({
			
			attributes: [
			  ['date','addTime'],
			  
			  [Sequelize.fn('sum', Sequelize.col('money2')), 'money3'],
			],
			group: ['addTime'],
			
			// attributes: {
			// 	//排除之前没有字段'createdAt',
			// 	exclude: ['id', 'updatedAt', 'version']
			// },
			// sort: ['date', 'descending'],
			order: [['date', 'DESC']],
			limit: 10000,
			// offset: (Number(page) - 1) * limit,
			where: sdata,
		});
		
		ctx.rest(totalAmount)
	})
	
	
	router.post('/sc/order/sc_day_total/list', async (ctx) => { // 首页
		var {
			title,
			date
		} = ctx.request.body;
		var limit = 10000;
		var page = 1;
		var sdata = {}
		
		let res2 = await sc_DBBASE.findAll({
			attributes: {
				//排除之前没有字段'createdAt',
				exclude: ['id', 'updatedAt', 'version']
			},
			// sort: ['date', 'descending'],
			order: [['date', 'DESC']],
			limit: limit,
			// offset: (Number(page) - 1) * limit,
			where: sdata,
		})
		// ctx.response.body = {
		// 	code: 200,
		// 	list: res2
		// }
		res2.forEach(async(e)=>{
			var money = await get_day_money(e.date)
			var data = {
				total:money
			}
			var userUpdate = await sc_DBBASE.update(data, {
				'where': {
					'date':e.date
				}
			})
			
		})
		
		
		let res3 = await sc_DBBASE.findAll({
			attributes: {
				//排除之前没有字段'createdAt',
				exclude: ['id', 'updatedAt', 'version']
			},
			// sort: ['date', 'descending'],
			order: [['date', 'DESC']],
			limit: limit,
			// offset: (Number(page) - 1) * limit,
			where: sdata,
		})
		ctx.rest(res3)
	})
	
	return router;
}
